package com.changgou.page.service.impl;
import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.GoodsFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private GoodsFeign goodsFeign;
    @Autowired
    private CategoryFeign  categoryFeign;
    @Value("${pagepath}")
    private  String pagepath;
    @Autowired
    private TemplateEngine templateEngine;
    @Override
    public void generateItemPage(String spuId) {
        //获取context对象用于存放商品详情的数据
        Context context = new Context();
        Map<String, Object> itemData  = this.findItemData(spuId);
        context.setVariables(itemData);
        //获取详情页生成的位置
        File file = new File(pagepath);
        //判断位置是否存在
        if(!file.exists()){
            file.mkdirs();
        }
        //定义输出流用于文件的生成
        File file1 = new File(file + "/" + spuId + ".html");

        Writer out = null;
        try{
            out = new PrintWriter(file1);
            //生成文件
            /**
             * 1.模板名称
             * 2.context对象,包含了模板需要的数据
             * 3.输出流,指定文件输出位置
             */
            templateEngine.process("item",context,out);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //获取静态化页面的数据
    private Map<String, Object> findItemData(String spuId) {
        Map<String, Object> map = new HashMap<>();
        //获取spu的信息
        Result<Goods> result = goodsFeign.findById(spuId);
        Goods goods = result.getData();
        Spu spu = goods.getSpu();
        map.put("spu", spu);
        //获取图片信息
        if(spu != null) {
            if(StringUtils.isNotEmpty(spu.getImages())) {
                String[] imageList = spu.getImages().split(",");
                map.put("imageList",imageList);
            }
        }
        //获取分类的信息
        Category category1  = categoryFeign.findById(spu.getCategory1Id()).getData();
        map.put("category1",category1);
        Category category2  = categoryFeign.findById(spu.getCategory2Id()).getData();
        map.put("category2",category2);
        Category category3  = categoryFeign.findById(spu.getCategory3Id()).getData();
        map.put("category3",category3);
        //获取sku集合
        List<Sku> skuList = goods.getSkuList();
        map.put("skuList",skuList);
        map.put("specificationList", JSON.parseObject(spu.getSpecItems(), Map.class));
        return map;
    }
}
